﻿using System;
using System.Collections.Generic;
using System.Text;
using Autorun;
using System.Net.Mail;

namespace Samples
{
    [Serializable]
    public class SendMail : TaskBase
    {
        #region "Constants"
        private const string SERVER = "Server";
        private const string PORT = "Port";
        private const string FROM = "From";
        private const string TO = "To";
        private const string SUBJECT = "Subject";
        private const string BODY = "Body";
        #endregion

        #region "Members"
        private MailAddress mailFrom;
        private MailAddress mailTo;
        private string mailSubject;
        private string mailBody;
        private string mailServer;
        private int mailPort;
        #endregion

        #region "Overriden Methods"
        protected override void InitializeParameterList()
        {
            Parameters.Add(FROM, "username@domain.com");
            Parameters.Add(TO, "username@domain.com");
            Parameters.Add(SUBJECT, "Mail Subject");
            Parameters.Add(BODY, "Mail Body");
            Parameters.Add(SERVER, "127.0.0.1");
            Parameters.Add(PORT, "25");
        }

        protected override void PreProcess()
        {
            System.Windows.Forms.MessageBox.Show("Test");
            // === Verify the parameters =====================================
            mailFrom = new MailAddress(Parameters[FROM]);
            mailTo = new MailAddress(Parameters[TO]);
            mailSubject = Parameters[SUBJECT];
            mailBody = Parameters[BODY];
            mailServer = Parameters[SERVER];
            mailPort = int.Parse(Parameters[PORT]);
            Notify(SeverityLevels.Verbose, "PreProcess() terminated");
        }

        protected override void Process()
        {
            SmtpClient client;

            client = new SmtpClient(mailServer.ToString(), mailPort);
            client.Send(mailFrom.ToString(), mailTo.ToString(), mailSubject, mailBody);
            Notify(SeverityLevels.Verbose, "Process() terminated");
        }

        protected override void PostProcess()
        {
            ExitCode = 0;
            Notify(SeverityLevels.Verbose, "PostProcess() terminated");
        }
        #endregion
    }
}
